package com.ossean.databaseSource;

import java.util.List;
import java.util.Map;

import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import com.ossean.model.GatherProjectsModel;

public interface GatherDao {
	
	//向gather_tasks表插入转移任务
	@Insert("INSERT ignore INTO ${taskTableName} (BeginId,EndId,SourceTableName,TargetTableName) VALUES (${idsBegin},${idsEnd},#{sourceTableName},#{targetTableName})")
    public int insertLog(@Param("idsBegin") int idsBegin, @Param("idsEnd") int idsEnd, @Param("taskTableName") String taskTableName,
                         @Param("sourceTableName") String sourceTableName, @Param("targetTableName") String targetTableName);

	//查询未处理的task
	@Select("SELECT BeginId,EndId,BeginTime,EndTime " +
            "FROM ${migrationTask} " +
            "WHERE IsDone = 0 AND SourceTableName=#{sourceTableName} AND TargetTableName=#{targetTableName} ORDER BY BeginId LIMIT 0,1")
    public Map checkDone(@Param("migrationTask") String migrationTask, @Param("sourceTableName") String sourceTableName, @Param("targetTableName") String targetTableName);

	//更新转移任务开始时间
	@Update("UPDATE ${migrationTask} SET BeginTime = NOW() WHERE BeginId = ${sourceIdBegin} AND SourceTableName=#{sourceTableName} AND TargetTableName=#{targetTableName}")
    public int updateTaskBeginTime(@Param("migrationTask") String migrationTask, @Param("sourceIdBegin") int sourceIdBegin, @Param("sourceTableName") String sourceTableName, @Param("targetTableName") String targetTableName);

	//获取数据源中相应数据 两边都有等于号
	@Select("Select ${selectItems} from ${sourceTable} " +
            "where id >=${idBegin} and id <= ${idEnd} ${andWhere}")
    public List<GatherProjectsModel> moveDataGet(@Param("sourceTable") String sourceTableName,
                                                 @Param("selectItems") String selectItems,
                                                 @Param("idBegin") int idBegin, @Param("idEnd") int idEnd,
                                                 @Param("andWhere") String andWhere);
	
	//将获取到得数据源数据插入目标表中
	@Insert("INSERT INTO ${targetTable} (${targetFields}) VALUES (#{model.id},#{model.deal_name},#{model.name},#{model.description},#{model.last_update_time},#{model.registered_time},#{model.followers_num},#{model.url},#{model.urlMD5},#{model.pageMD5},#{model.language},#{model.tags},#{model.contributors_num},#{model.monitor},#{model.status},#{model.download_num},#{model.visit_num},#{model.category},#{model.crawled_time},#{model.history},#{model.source},#{model.license},#{model.platform},#{model.homepage})")
    public int moveDataInsert(@Param("targetTable") String targetTableName,
                              @Param("targetFields") String targetFields,
                              @Param("model") GatherProjectsModel model);
	
	//对urlMD5相同的数据进行replace into操作
	@Update("replace into ${targetTable} (${targetFields}) values (#{model.deal_name},#{model.name},#{model.description},#{model.last_update_time},#{model.registered_time},#{model.followers_num},#{model.url},#{model.urlMD5},#{model.pageMD5},#{model.language},#{model.tags},#{model.contributors_num},#{model.monitor},#{model.status},#{model.download_num},#{model.visit_num},#{model.category},#{model.crawled_time},#{model.history},#{model.source},#{model.license},#{model.platform},#{model.homepage})")
	public int moveDataReplace(@Param("targetTable") String targetTableName,
								@Param("targetFields") String targetFields,
								@Param("model") GatherProjectsModel model);
	
	//对urlMD5相同的数据进行update操作
	@Update("update ${targetTable} set deal_name=#{model.deal_name},name=#{model.name},description=#{model.description},"
			+ "last_update_time=#{model.last_update_time},registered_time=#{model.registered_time},"
			+ "followers_num=#{model.followers_num},pageMD5=#{model.pageMD5},language=#{model.language},"
			+ "tags=#{model.tags},contributors_num=#{model.contributors_num},monitor=#{model.monitor},"
			+ "status=#{model.status},download_num=#{model.download_num},visit_num=#{model.visit_num},"
			+ "category=#{model.category},crawled_time=#{model.crawled_time},history=#{model.history},"
			+ "source=#{model.source},license=#{model.license},platform=#{model.platform},homepage=#{model.homepage} where id=#{id}")
	public void moveDataUpdate(@Param("targetTable") String targetTableName, @Param("model") GatherProjectsModel model, @Param("id") int id);
	
	
//	//获取刚刚插入gather_projects表中数据的id 由于gather_projects表中的自增id主键删除
//	@Select("select id from ${table} where urlMD5=#{model.urlMD5} and name=#{model.name} order by id desc limit 1")
//	public int getAutoIncrementIdFromTable(@Param("table") String table, @Param("model") GatherProjectsModel model);
	
	
	//更新数据
	@Update("")
	public void updateData(@Param("id") int id, @Param("model") GatherProjectsModel model);
	
	
	//更新任务结束时间
	@Update("UPDATE ${migrationTask} SET IsDone = 1,EndTime = NOW(),flow=${moveNum} WHERE BeginId = ${endIdBegin} AND SourceTableName=#{sourceTableName} AND TargetTableName=#{targetTableName}")
    public int updateTaskEndTime(@Param("migrationTask") String migrationTask, @Param("endIdBegin") int endIdBegin, @Param("moveNum") int moveNum, @Param("sourceTableName") String sourceTableName, @Param("targetTableName") String targetTableName);
	
	
	//向tag表存储数据
	@Insert("insert ignore into ${table} (name) values (#{name})")
	public void insertTag(@Param("table") String table, @Param("name") String name);
	
	//根据tag的name找tag的id
	@Select("select id from ${table} where name=#{name}")
	public int selectTagIdByName(@Param("table") String table, @Param("name") String name);
	
	//根据urlMD5码找到GatherProjectModel对象
	@Select("select * from ${table} where urlMD5=#{urlMD5}")
	public GatherProjectsModel selectGPMByUrlMD5(@Param("table") String table, @Param("urlMD5") String urlMD5);
	
	
	//根据id和limit取GatherProjectsModel列表
	//@Select("select * from ${table} where id>=#{start} and id<=#{maxId} order by id asc limit #{size}")
	//public List<GatherProjectsModel> selectGPMList(@Param("table") String table, @Param("start") int start, @Param("size") int size, @Param("maxId") int maxId);
	@Select("select * from ${table} where  update_mark = 0  limit #{size}")
	public List<GatherProjectsModel> selectGPMList(@Param("table") String table, @Param("size") int size);
	
	@Select("select * from ${table} where id>=#{start} and id<#{end} limit #{size}")
	public List<GatherProjectsModel> selectGPMListBySize(@Param("table") String table, @Param("start") int start, @Param("end") int end, @Param("size") int size);

	//根据id取GatherProjectsModel对象
	@Select("select * from ${table} where id=#{id}")
	public GatherProjectsModel selectGPMById(@Param("table") String table, @Param("id") int id);
	
	//查找相同homepage的项目 但不能是正在处理的项目本身
	@Select("select * from ${table} where (homepage=#{homepage} or homepage=#{homepage1} or homepage=#{homepage2} or homepage=#{homepage3} or homepage=#{homepage4}) and id!=#{id}")
	public List<GatherProjectsModel> selectGPMBySameHomePage(@Param("table") String table, @Param("homepage") String homepage, @Param("homepage1") String homepage1, @Param("homepage2") String homepage2, @Param("homepage3") String homepage3, @Param("homepage4") String homepage4, @Param("id") int id, @Param("source") String source);

	//查找相同name的项目
	@Select("select * from ${table} where name=#{name} and id!=#{id}")
	public List<GatherProjectsModel> selectGPMBySameName(@Param("table") String table, @Param("name") String name, @Param("id") int id);
	
	
	//取出所有update_mark为2的更新数据
	@Select("select * from ${table} where update_mark=2 limit #{size}")
	public List<GatherProjectsModel> selectUpdateProjects(@Param("table") String table,@Param("size") int size);
	
	//更新update_mark字段
	@Select("update ${table} set update_mark=#{update_mark} where id=#{id}")
	public void updateUpdateMark(@Param("table") String table, @Param("update_mark") int update_mark, @Param("id") int id);
	
}